package cn.tmpan.project.service.impl;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.tmpan.project.dao.NfzhMapper;
import cn.tmpan.project.model.Jtcyxx;
import cn.tmpan.project.dao.JtcyxxMapper;
import cn.tmpan.project.model.Nfzh;
import cn.tmpan.project.model.vo.FamilyInfo;
import cn.tmpan.project.service.IJtcyxxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 家庭成员电话 服务实现类
 * </p>
 *
 * @author AutoGenerator
 */
@Service
public class JtcyxxServiceImpl extends ServiceImpl<JtcyxxMapper, Jtcyxx>implements IJtcyxxService {

    @Autowired
    private JtcyxxMapper jtcyxxMapper;

    @Autowired
    private NfzhMapper nfzhMapper;

    /**
     * 新增家庭成员信息
     *
     * @param jtcyxm  家庭成员姓名
     * @param jtcysfz 家庭成员身份证
     * @param jtcydh  家庭成员电话号码
     * @param sfzhm   户主身份证号码
     * @return
     */
    @Override
    public Boolean insertJtcyxx(String jtcyxm, String jtcysfz, String jtcydh, String sfzhm) throws Exception {
        List<Nfzh> nfzhs = this.nfzhMapper.selectNfzhBySfzhm(sfzhm);
        if (ObjectUtil.isNull(nfzhs) || nfzhs.size() < 1){
            throw new Exception("户主身份证号码有误");
        }

        Jtcyxx jtcyxx = new Jtcyxx();
        jtcyxx.setJtcyxm(jtcyxm);
        jtcyxx.setJtcysfz(jtcysfz);
        jtcyxx.setJtcydh(jtcydh);
        jtcyxx.setSfzhm(sfzhm);
        int r = this.jtcyxxMapper.insert(jtcyxx);
        return r > 0;
    }

    /**
     * 修改家庭成员信息
     *
     * @param bm      编码
     * @param jtcyxm  家庭成员姓名
     * @param jtcysfz 家庭成员身份证
     * @param jtcydh  家庭成员电话号码
     * @return
     * @throws Exception
     */
    @Override
    public Boolean updateJtcyxx(String bm, String jtcyxm, String jtcysfz, String jtcydh) throws Exception {
        Jtcyxx jtcyxx = this.jtcyxxMapper.selectById(bm);
        if (ObjectUtil.isNull(jtcyxx)){
            throw new Exception("该家庭成员不存在");
        }

        jtcyxx.setJtcyxm(jtcyxm);
        jtcyxx.setJtcysfz(jtcysfz);
        jtcyxx.setJtcydh(jtcydh);
        int r = this.jtcyxxMapper.updateById(jtcyxx);
        return r > 0;
    }

    /**
     * 根据户主身份证号码查询家庭成员
     *
     * @param sfzhm 户主身份证号码
     * @return
     */
    @Override
    public List<Jtcyxx> selectJtcyxxBySfzhm(String sfzhm) throws Exception {
        List<Nfzh> nfzhs = this.nfzhMapper.selectNfzhBySfzhm(sfzhm);
        if (ObjectUtil.isNull(nfzhs) || nfzhs.size() < 1){
            throw new Exception("户主身份证号码有误");
        }


        return this.jtcyxxMapper.selectJtcyxxBySfzhm(sfzhm);
    }

    /**
     * 删除家庭成员信息
     *
     * @param bm 编码
     * @return
     */
    @Override
    public Boolean deleteJtcyxx(String bm) throws Exception {
        Jtcyxx jtcyxx = this.jtcyxxMapper.selectById(bm);
        if (ObjectUtil.isNull(jtcyxx)){
            throw new Exception("该家庭成员不存在");
        }

        int r = this.jtcyxxMapper.deleteById(bm);
        return r > 0;
    }

    @Override
    public void downloadExcel(HttpServletResponse response, HttpServletRequest request){
        List<FamilyInfo> familyInfos = this.jtcyxxMapper.selectFamilyInfo();
        ExportParams exportParams = new ExportParams("家庭成员信息", "家庭成员信息表", ExcelType.XSSF);
        exportParams.setCreateHeadRows(true);
        defaultExport(familyInfos,FamilyInfo.class,"家庭成员信息表.xlsx",response,exportParams);
    }

    private  void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
        if (workbook != null);
        downLoadExcel(fileName, response, workbook);
    }

    private  void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
           e.printStackTrace();
        }
    }
}
